<template>
  <div>
    <el-menu
        :default-active="currentPath"
        router
        mode="horizontal"
        background-color="white"
        text-color="#222"
        active-text-color="red"
        style="min-width: 1300px">
      <el-menu-item v-for="(item, i) in navList" :key="i" :index="item.name">
        {{ item.navItem }}
      </el-menu-item>
      <!--<a href="#nowhere" style="color: #222;float: right;padding: 20px;">更多功能</a>-->
      <!--<i class="el-icon-menu" style="float: right;font-size: 45px;color: #222;padding-top: 8px"></i>-->
      <span style="position: absolute;padding-top: 20px;right: 43%;font-size: 20px;font-weight: bold">Wind - Your Mind Palace</span>
      <el-input placeholder="快速搜索..." prefix-icon="el-icon-search" size="medium"
                style="width: 300px;position:absolute;margin-top: 12px;right: 18%" v-model="keywords"></el-input>
      <i class="el-icon-switch-button" v-on:click="logout"
         style="float: right;font-size: 40px;color: #222;padding: 10px"></i>
    </el-menu>
  </div>
</template>

<script>
export default {
  name: 'NavMenu',
  data() {
    return {
      navList: [
        {name: '/home', navItem: '首页'},
        {name: '/article', navItem: '笔记本'},
        {name: '/library', navItem: '图书馆'},
        {name: '/admin', navItem: '个人中心'}
      ],
      keywords: ''
    }
  },
  computed: {
    hoverBackground() {
      return '#ffd04b'
    },
    currentPath() {
      let x = this.$route.path.indexOf('/', 1)
      if (x !== -1) {
        return this.$route.path.substring(0, x)
      } else {
        return this.$route.path
      }
    }
  },
  methods: {
    logout() {
      let _this = this
      this.$axios.post('/api/logout').then(response => {
        if (response.data.code === 200) {
          // 前后端状态保持一致
          _this.$store.commit('logout')
          let path = this.$route.query.redirect;
          this.$router.replace((path == '/' || path == undefined) ? '/login' : path);
        }
      }).catch(function (error) {
        _this.loading = false;
        console.log('服务器异常' + error)
      })
    }
  }
}
</script>

<style scoped>
a {
  text-decoration: none;
}

span {
  pointer-events: none;
}

.el-icon-switch-button {
  cursor: pointer;
  outline: 0;
}
</style>
